package com.game.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.business.domain.*;
import com.game.business.mapper.TOrderDbrRecordMapper;
import com.game.business.service.*;
import com.game.common.core.domain.entity.SysUser;
import com.game.common.utils.DateUtils;
import com.game.common.utils.SecurityUtils;
import com.game.common.utils.StringUtils;
import com.game.system.service.ISysUserService;
import com.game.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.TOrderMapper;
import org.springframework.util.CollectionUtils;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-13
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper,TOrder>  implements ITOrderService
{
    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITOrderDbrRecordService orderDbrRecordService;

    @Autowired
    private ITGameService gameService;

    @Autowired
    private ITGroupService groupService;



    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public TOrder selectTOrderById(String id)
    {
        TOrder tOrder1 = tOrderMapper.selectTOrderById(id);
        setOrder(tOrder1);;
        QueryWrapper<TGroup>  groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.eq("order_id",tOrder1.getId());
        List<TGroup> grouplist = groupService.list(groupQueryWrapper);
        if(!CollectionUtils.isEmpty(grouplist)){
            tOrder1.setGroup(grouplist.get(0));
        }else{
            tOrder1.setGroup(null);
        }
        return tOrder1;
    }

    /**
     * 查询订单列表
     *
     * @param tOrder 订单
     * @return 订单
     */
    @Override
    public List<TOrder> selectTOrderList(TOrder tOrder)
    {
        List<TOrder> tOrders = tOrderMapper.selectTOrderList(tOrder);
        for (int i = 0; i <tOrders.size() ; i++) {
            TOrder tOrder1 = tOrders.get(i);
            setOrder(tOrder1);

        }

        return tOrders;
    }

    @Autowired
    private ITOrderNoticeService  orderNoticeService;

    public  void setOrder(TOrder  tOrder1){
        String playUserId = tOrder1.getPlayUserId();
        if(StringUtils.isNotEmpty(playUserId)){
            SysUser sysUser = sysUserService.selectUserById(Long.valueOf(playUserId));
            // 打手
            tOrder1.setPlayUser(sysUser);
        }
        String userId = tOrder1.getUserId();
        SysUser sysUser1 = sysUserService.selectUserById(Long.valueOf(userId));
        // 发单人
        tOrder1.setUser(sysUser1);
        // 担保人
        TOrderDbrRecord dbrRecord = new TOrderDbrRecord();
        dbrRecord.setOrderId(tOrder1.getId());
        List<TOrderDbrRecord> tOrderDbrRecords = orderDbrRecordService.selectTOrderDbrRecordList(dbrRecord);
        tOrder1.setDbrUserList(tOrderDbrRecords);
        String gameId = tOrder1.getGameId();
        TGame game = gameService.getById(gameId);
        tOrder1.setGame(game);
        if(StringUtils.isNotEmpty(SecurityUtils.getUserIdStrSS())){
            // 是否报价过
            QueryWrapper<TOrderNotice>  tOrderNoticeQueryWrapper = new QueryWrapper<>();
            tOrderNoticeQueryWrapper.eq("order_id",tOrder1.getId());
            tOrderNoticeQueryWrapper.eq("type","2");
            tOrderNoticeQueryWrapper.eq("send_user_id",SecurityUtils.getUserIdStr() );
            long count = orderNoticeService.count(tOrderNoticeQueryWrapper);
            tOrder1.setIsyjBj(count>0);
        }

    }
    /**
     * 新增订单
     *
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int insertTOrder(TOrder tOrder)
    {
                tOrder.setCreateTime(DateUtils.getNowDate());
            return tOrderMapper.insertTOrder(tOrder);
    }

    /**
     * 修改订单
     *
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int updateTOrder(TOrder tOrder)
    {
                tOrder.setUpdateTime(DateUtils.getNowDate());
        return tOrderMapper.updateTOrder(tOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByIds(String[] ids)
    {
        return tOrderMapper.deleteTOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderById(String id)
    {
        return tOrderMapper.deleteTOrderById(id);
    }

    @Override
    public TOrder getOrderByOrderNo(String orderNo) {
        LambdaQueryWrapper<TOrder> orderquery = new LambdaQueryWrapper<TOrder>().eq(TOrder::getOrderNo, orderNo);
        TOrder order = this.getOne(orderquery);
        return order;

    }
}
